#! /usr/bin/python

def map_tc_to_file(tc, title, filename):
	# 'tc' is in TC format: see tc.py for info
	data = ''

	# print header
	data += '[header]\n'
	data += 'title=' + title + '\n'
	data += 'width=' + str(len(tc['collision'][0])) + '\nheight=' + str(len(tc['collision'])) + '\n'
	data += 'tileset=tileset_amits.txt\n'
	data += 'spawnpoint=0,0,0\n'
	data += 'music=unrest_theme.ogg\n\n'
	
	# print layer info
	for layer in ['background','object','collision']:
		data += '[layer]\n'
		data += 'id=' + layer + '\n'
		data += 'format=dec\n'
		data += 'data=\n'
		data += csv(tc[layer]) + '\n\n'

	#print dimmy info
	for line in tc['dimmies']:
		if line[0] != '#': #don't print comments
			if line[0] == '[':
				data += '\n'
			data += line.replace('}','').replace('{','') + '\n'

	# write data
	f = open(filename, 'w')
	f.write(data)
	f.close()
	print "Successfully exported file: " + filename + "."

# TURNS A 2D ARRAY INTO A CSV STRING
def csv(data):
	csv_data = ''
	for row in data:
		for col in row:
			csv_data += col + ','
		csv_data = csv_data.strip(',') + '\n'
	return csv_data.strip()
	
